<template>
  <div>
    <el-tree
      ref="tree"
      node-key="id"
      check-strictly
      show-checkbox
      :data="list"
      :props="{label:'name'}"
    />
    <el-row>
      <el-button type="success" size="mini" @click="hSubmit">成功按钮</el-button>
      <el-button type="info" size="mini">取消按钮</el-button>
    </el-row>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permisson'
import { tranDataToTreeData } from '@/utils/index'
import { getRoleDetail, assignPerm } from '@/api/settings'
export default {
  name: 'VueHrAssignPermission',
  props: {
    curId: {
      type: String,
      required: true
    }
  },

  data() {
    return {
      list: []
    }
  },
  created() {
    this.loadPermissionList()
  },
  methods: {
    async hSubmit() {
      try {
        const permIds = this.$refs.tree.getCheckedKeys()
        await assignPerm({ id: this.curId, permIds: permIds })
        // 子传父 关闭弹窗
        this.$emit('success')
        // console.log(res)
      } catch (error) {
        console.log(error)
      }
    },
    // 发送 获得 权限列表请求
    async loadPermissionList() {
      try {
        const res = await getPermissionList()
        // console.log(res)
        // 发送获得 员工本身用的权限点，并让其选中
        const delem = await getRoleDetail(this.curId)
        console.log(delem.data.permIds)
        console.log(delem, 'delem')
        this.$refs.tree.setCheckedKeys(delem.data.permIds)
        this.list = tranDataToTreeData(res.data)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
